<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getsimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    //   由父组件传来的值
    id: {
      type: String,
      required: true
    },
    // 接收是否编辑数据的值
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      employees: [], // 员工简单列表
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    id(newVal, oldVal) {
      console.log('id变化了', oldVal, '--->', newVal)
      this.loadDetail()
    }
  },
  created() {
    this.setsimple() // 获取员工简单列表
    this.loadDetail()
  },
  methods: {
    async setsimple() {
      try {
        const rs = await getsimple()
        console.log(rs)
        this.employees = rs.data
      } catch (err) {
        console.log(err)
      }
    },
    // 获取当前部门的详情
    async loadDetail() {
      if (this.isEdit) {
        // alert('现在是编辑，要去获取详情')
        const res = await getDepartDetail(this.id)
        console.log('现在是编辑，要去获取详情', res)
        this.form = res.data
      }
    },
    async addFn() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
        this.$emit('update-depart')
      } catch (err) {
        console.log(err)
      }
    },
    async EditFn() {
      try {
        await updateDepartments(this.form)
        this.$message({ type: 'success', message: '修改成功' })
        this.$emit('update-depart')
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: err })
      }
    },
    hSubmit() {
      // 表单验证
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.EditFn() : this.addFn()
        }
      })
      //   点击确认时从父系组件调用重新加载的方法
    },
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
